package person.twj.session.core.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;

/**
 * 登录成功后，操作
 */
public class MyLoginSuccessHandler implements AuthenticationSuccessHandler {
    private RedirectStrategy redirectStrategy;
    public MyLoginSuccessHandler() {
        this.redirectStrategy = new DefaultRedirectStrategy();
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {

        //@TODO 业务操作，例如，成功登录后，加载权限信息到session缓存

        request.getSession().setAttribute("msg","hello world!");

        this.redirectStrategy.sendRedirect(request,response,"/");

    }
}
